package com.dawn.module.cms.controller.app.doctor;

import com.dawn.framework.common.enums.CommonStatusEnum;
import com.dawn.framework.common.pojo.CommonResult;
import com.dawn.framework.common.pojo.PageParam;
import com.dawn.framework.common.pojo.PageResult;
import com.dawn.framework.common.util.object.BeanUtils;
import com.dawn.module.cms.controller.admin.article.vo.ArticleSaveReqVO;
import com.dawn.module.cms.controller.admin.doctor.vo.DoctorPageReqVO;
import com.dawn.module.cms.controller.admin.doctor.vo.DoctorRespVO;
import com.dawn.module.cms.controller.admin.doctor.vo.DoctorSaveReqVO;
import com.dawn.module.cms.dal.dataobject.doctor.DoctorDO;
import com.dawn.module.cms.service.doctor.DoctorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.List;

import static com.dawn.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 APP - 医生信息")
@RestController
@RequestMapping("/cms/doctor")
@Validated
public class AppDoctorController {

    @Resource
    private DoctorService doctorService;


    @GetMapping("/get")
    @Operation(summary = "获得医生信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<DoctorRespVO> getDoctor(@RequestParam("id") Long id) {
        DoctorDO doctor = doctorService.getDoctor(id);

        DoctorSaveReqVO doctorSaveReqVO = new DoctorSaveReqVO();
        doctorSaveReqVO.setId(doctor.getId());
        int views = 1;
        if(doctor.getViews() != null){
            views += doctor.getViews();
        }
        doctorSaveReqVO.setViews(views);
        doctorSaveReqVO.setName(doctor.getName());
        doctorSaveReqVO.setSort(doctor.getSort());
        doctorSaveReqVO.setStatus(doctor.getStatus());
        doctorService.updateDoctor(doctorSaveReqVO);

        return success(BeanUtils.toBean(doctor, DoctorRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得医生信息分页")
    public CommonResult<PageResult<DoctorRespVO>> getDoctorPage(@Valid DoctorPageReqVO pageReqVO) {
        pageReqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
        pageReqVO.setPublish(CommonStatusEnum.ENABLE.getStatus());
        PageResult<DoctorRespVO> pageResult = doctorService.getDoctorPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DoctorRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得医生列表")
    public CommonResult<List<DoctorRespVO>> getDoctorList(@Valid DoctorPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        pageReqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
        pageReqVO.setPublish(CommonStatusEnum.ENABLE.getStatus());
        List<DoctorRespVO> list = doctorService.getDoctorPage(pageReqVO).getList();
        return success(BeanUtils.toBean(list, DoctorRespVO.class));
    }

}